package com.tnovoselec.android.common.ui.tabbar;

import java.util.ArrayList;
import java.util.List;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.tnovoselec.android.common.R;
import com.tnovoselec.android.common.util.ScreenUtil;


public class TabActivity extends ActivityGroup implements RadioGroup.OnCheckedChangeListener {

	public static String CURRENT_TAB_INDEX;
	public static String ACTION_CHANGE_TAB = "com.nth.common.changeTab";

	private LocalActivityManager activityManager;
	private LinearLayout contentViewLayout;
	private LinearLayout.LayoutParams contentViewLayoutParams;
	@SuppressWarnings("unused")
	private HorizontalScrollView bottomBar;
	private RadioGroup.LayoutParams buttonLayoutParams;
	private RadioGroup bottomRadioGroup;

	private List<Intent> intentList = null;
	private List<String> titleList = null;
	private List<int[]> states = null;

	private SliderBarActivityDelegate delegate = null;

	private int defaultOffShade = RadioStateDrawable.SHADE_GRAY;
	private int defaultOnShade = RadioStateDrawable.SHADE_GREEN;

	private IntentFilter changeTabIntentFilter;
	private ChangeTabBroadcastReceiver changeTabBroadcastReceiver;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		activityManager = super.getLocalActivityManager();
		super.setContentView(R.layout.tab_host);
		contentViewLayout = (LinearLayout) super.findViewById(R.id.content_view_layout);
		bottomBar = (HorizontalScrollView) super.findViewById(R.id.bottom_bar);
		bottomRadioGroup = (RadioGroup) super.findViewById(R.id.bottom_menu);
		bottomRadioGroup.setOnCheckedChangeListener(this);
		contentViewLayoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

		intentList = new ArrayList<Intent>();
		titleList = new ArrayList<String>();
		states = new ArrayList<int[]>();

		buttonLayoutParams = new RadioGroup.LayoutParams(320 / 5, LayoutParams.WRAP_CONTENT);
	}

	@Override
	public void onResume() {
		changeTabIntentFilter = new IntentFilter(TabActivity.ACTION_CHANGE_TAB);
		changeTabBroadcastReceiver = new ChangeTabBroadcastReceiver();
		registerReceiver(changeTabBroadcastReceiver, changeTabIntentFilter);
		super.onResume();
	}

	@Override
	public void onPause() {
		unregisterReceiver(changeTabBroadcastReceiver);
		super.onPause();
	}

	public void commit() {
		bottomRadioGroup.removeAllViews();

		int optimumVisibleItemsInPortraitMode = 5;
		try {
			optimumVisibleItemsInPortraitMode = (int) (ScreenUtil.getScreenWidth(this) / 64.0);
		} catch (Exception ex) {
			optimumVisibleItemsInPortraitMode = 5;
		}

		int width;
		if (intentList.size() <= optimumVisibleItemsInPortraitMode) {
			width = ScreenUtil.getScreenWidth(this) / intentList.size();
		} else {
			// width = screen_width/5;
			width = 480 / 5;
		}

		buttonLayoutParams = new RadioGroup.LayoutParams(width, LayoutParams.WRAP_CONTENT);

		for (int i = 0; i < intentList.size(); i++) {
			TabBarButton tabButton = new TabBarButton(this);
			int[] iconStates = states.get(i);
			if (iconStates.length == 1) {
				tabButton.setState(titleList.get(i).toString(), iconStates[0], width);
			} else if (iconStates.length == 2) {
				tabButton.setState(iconStates[0], iconStates[1]);
			} else if (iconStates.length == 3) {
				tabButton.setState(titleList.get(i).toString(), iconStates[0], width, iconStates[1], iconStates[2]);
			}
			tabButton.setId(i);
			tabButton.setGravity(Gravity.CENTER);

			bottomRadioGroup.addView(tabButton, i, buttonLayoutParams);
		}

		setCurrentTab(0);
	}

	/**
	 * <b><i>protected void addTab(String title, int offIconStateId, int onIconStateId, Intent intent)</i></b>
	 * <p><p>
	 * Add a tab to the navigation bar by specifying the title, 1 image for button on-state, and 1 image for button off-state.
	 * <p>
	 * 
	 * @param title				a String that specifies that title of the tab button
	 * @param onIconStateId		id of the on-state image
	 * @param offIconStateId	id of the off-state image
	 * @param intent			intent to start when button is tapped
	 */
	protected void addTab(String title, int offIconStateId, int onIconStateId, Intent intent) {
		int[] iconStates = { onIconStateId, offIconStateId };
		states.add(iconStates);
		intentList.add(intent);
		titleList.add(title);
		// commit();
	}

	/**
	 * <b><i>protected void addTab(String title, int iconStateId, Intent intent)</i></b>
	 * <p><p>
	 * Add a tab to the navigation bar by specifying the title, and 1 image for the button. Default green/gray shade is used for button on/off state.
	 * <p>
	 * 
	 * @param title				a String that specifies that title of the tab button
	 * @param iconStateId		id of the image used for both on/off state
	 * @param intent			intent to start when button is tapped
	 */
	protected void addTab(String title, int iconStateId, Intent intent) {
		// int[] iconStates = {iconStateId};
		int[] iconStates = { iconStateId, defaultOffShade, defaultOnShade };
		states.add(iconStates);
		intentList.add(intent);
		titleList.add(title);
		// commit();
	}

	/**
	 * <b><i>protected void addTab(String title, int iconStateId, int offShade, int onShade, Intent intent)</i></b>
	 * <p><p>
	 * Add a tab to the navigation bar by specifying the title, and 1 image for the button. Default green/gray shade is used for button on/off state.
	 * <p>
	 * 
	 * @param title				a String that specifies that title of the tab button
	 * @param iconStateId		id of the image used for both on/off state
	 * @param offShade  		id for off-state color shades (e.g. RadioStateDrawable.SHADE_GRAY, RadioStateDrawable.SHADE_GREEN etc)
	 * @param onShade			id for on-state color shades (e.g. RadioStateDrawable.SHADE_GRAY, RadioStateDrawable.SHADE_GREEN etc)
	 * @param intent			intent to start when button is tapped
	 */
	protected void addTab(String title, int iconStateId, int offShade, int onShade, Intent intent) {
		int[] iconStates = { iconStateId, offShade, onShade };
		states.add(iconStates);
		intentList.add(intent);
		titleList.add(title);
		// commit();
	}

	/**
	 * <b><i>protected void setDefaultShde(int offShade, int onShade)</i></b>
	 * <p><p>
	 * Sets the default on and off color shades of the bottom bar buttons.
	 * <p>
	 * If not specified, the default off shade is gray, and the default on shade is green.
	 * 
	 * @param offShade			id for off-state color shades (e.g. RadioStateDrawable.SHADE_GRAY, RadioStateDrawable.SHADE_GREEN etc)
	 * @param onShade			id for on-state color shades (e.g. RadioStateDrawable.SHADE_GRAY, RadioStateDrawable.SHADE_GREEN etc)
	 */
	protected void setDefaultShade(int offShade, int onShade) {
		defaultOffShade = offShade;
		defaultOnShade = onShade;
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		try {
			if (delegate != null) {
				delegate.onTabChanged(checkedId);
			}
		} catch (Exception ex) {
			// DO nothing
		}

		startGroupActivity(titleList.get(checkedId).toString(), intentList.get(checkedId));
	}

	public void startGroupActivity(String id, Intent intent) {
		contentViewLayout.removeAllViews();

		View view = activityManager.startActivity(id, intent).getDecorView();
		contentViewLayout.addView(view, contentViewLayoutParams);
	}

	public void setCurrentTab(int index) {
		bottomRadioGroup.check(index);
		startGroupActivity(titleList.get(index).toString(), intentList.get(index));
	}

	public int getCurrentTab() {
		return bottomRadioGroup.getCheckedRadioButtonId();
	}

	public boolean inflateXMLForContentView() {
		return false;
	}

	public int bottomBar() {
		return -1;
	}

	public void setDelegate(SliderBarActivityDelegate delegate_) {
		delegate = delegate_;
	}

	public static abstract class SliderBarActivityDelegate {

		protected abstract void onTabChanged(int tabIndex);
	}

	public class ChangeTabBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			int index = intent.getExtras().getInt(TabActivity.CURRENT_TAB_INDEX);
			setCurrentTab(index);
		}
	}
}
